package model.util;

public class CodingUtil {
	/**
	 * Coding from amplitude to sign phase
	 * 
	 * @param
	 * @return 0(min value) or PI(max value)
	 * 
	 * */
	public static double CodingFromAmplitudeToSignPhase(double amplitude) {
		return amplitude < 0 ? Math.PI : 0;
	}

	/**
	 * Coding from phase to sign 2 pi*m
	 * 
	 * @param phase
	 *            - phase (seriously)
	 * @param m
	 *            - то на что умножаем Пи (cannot in english) (default 1)
	 * @return -1 if shit happens
	 * 
	 * */
	public static double CodingPhaseTo2Pi(double phase, double MaxValueDoe, double MinValueDoe, double m) {

		double M2piStep = 2 * Math.PI * m;
		double M2piOffset = M2piStep - fmod_(MaxValueDoe, M2piStep) - .001;

		
		// if (phase >= MinValueDoe) {
		return Math.abs(fmod_((phase + M2piOffset), M2piStep));
		// } else {
		// ret = -1;
		// }

	}

	private static double fmod_(double a, double b) {
		int i = (int) (a / b);
		double t = a - i * b;
		if (t < 0) {
			t += b;
		}
		return t;
	}

	/**
	 * Coding from phase to amplitude (for what i dont know)
	 * 
	 * @param M__VF
	 *            - U-Frequency (1/mm) (default 3)
	 * @param M__VF
	 *            - V-Frequency (1/mm) (default 5)
	 * @return -1 if shit happens
	 * 
	 * */
	public static double CodingPhaseToAmplitude(double u, double v, double a, double M__UF, double M__VF) {
		return Math.cos(a - 2 * Math.PI * (M__UF * u + M__VF * v));

	}

	/**
	 * Coding from amplitude to phase with carrier
	 * 
	 * @param M__VF
	 *            - U-Frequency (1/mm) (default 30)
	 * @param M__VF
	 *            - V-Frequency (1/mm) (default 30)
	 * @param wave
	 *            - Useful Order (1 or 0)
	 * @param order
	 *            - Rect or sin (1 or 0)
	 * @return -1 if shit happens
	 * 
	 * */
	public static double CodingAmplToPhaseCarrier(double M__UF, double M__VF, int wave, int order) {

		if ((wave == 0) && (order == 0)) {
			return CodingAmplToPhaseCarrierFunk0();
		}
		if ((wave == 1) && (order == 0)) {
			return CodingAmplToPhaseCarrierFunh0();
		}
		if ((wave == 0) && (order == 1)) {
			return CodingAmplToPhaseCarrierFunk1();
		}
		if ((wave == 1) && (order == 1)) {
			return CodingAmplToPhaseCarrierFunh1();
		}
		return 0;
	}

	private static double CodingAmplToPhaseCarrierFunk0() {
		// TODO: implement later
		return 0;
	}

	private static double CodingAmplToPhaseCarrierFunh0() {
		// TODO: implement later
		return 0;
	}

	private static double CodingAmplToPhaseCarrierFunk1() {
		// TODO: implement later
		return 0;
	}

	private static double CodingAmplToPhaseCarrierFunh1() {
		// TODO: implement later
		return 0;
	}

	/**
	 * Coding from amplitude to amplitude with carrier
	 * 
	 * @param M__VF
	 *            - U-Frequency (1/mm) (default 30)
	 * @param M__VF
	 *            - V-Frequency (1/mm) (default 30)
	 * 
	 */
	public static double CodingAmplToAmplCarrier(double u, double v, double a, double M__UF, double M__VF) {
		return a * Math.cos(2 * Math.PI * (M__UF * u + M__VF * v));
	}

	/**
	 * Coding from amplitude to absolute amplitude
	 * */
	public static double CodingAmplToAbsAmpl(double amplitude) {
		return Math.abs(amplitude);
	}

	/**
	 * Coding from amplitude to intensity
	 * */
	public static double CodingAmplToIntensity(double amplitude) {
		return amplitude * amplitude;
	}
}
